package com.gejia.qixishare.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gejia.qixishare.entity.FileMd5RecordDO;
import com.gejia.qixishare.mapper.FileMd5RecordMapper;
import com.gejia.qixishare.service.FileMd5RecordService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zy
 * @since 2020-08-23
 */
@Service
public class FileMd5RecordServiceImpl extends ServiceImpl<FileMd5RecordMapper, FileMd5RecordDO> implements FileMd5RecordService {

    @Override
    public String getOSSPathIfExist(MultipartFile file) {
        QueryWrapper<FileMd5RecordDO> wrapper=new QueryWrapper<>();
        String md5Hex = null;
        try {
            md5Hex = DigestUtils.md5Hex(file.getInputStream());
            wrapper.eq("md5", md5Hex);
            FileMd5RecordDO recordDO = this.getOne(wrapper);
            if(recordDO != null){
                return recordDO.getFileOssPath();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
